package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.teleprompter.lib.db.TaiciBean;
import com.stark.teleprompter.lib.db.TaiciDbHelper;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LinesAdapter;
import flc.ast.databinding.ActivityMyLinesBinding;
import stark.common.basic.event.EventStatProxy;

public class MyLinesActivity extends BaseAc<ActivityMyLinesBinding> {

    private List<TaiciBean> mTaiciBeanList;
    private LinesAdapter linesAdapter;
    private Dialog myAddDialog;
    private EditText dialogText;

    @Override
    protected int onCreate() {
        return R.layout.activity_my_lines;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivMyLinesBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvLinesList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mDataBinding.rvLinesList.setItemAnimator(new DefaultItemAnimator());
        linesAdapter = new LinesAdapter(mContext);
        mDataBinding.rvLinesList.setAdapter(linesAdapter);
        linesAdapter.setOnItemClickListener(new LinesAdapter.OnItemClickListener() {
            @Override
            public void onClick(TaiciBean bean, int position) {

            }

            @Override
            public void onClickDelete(TaiciBean bean, int position) {
                List<TaiciBean> listDel = new ArrayList<>();
                listDel.add(bean);
                TaiciDbHelper.delete(listDel);
                getData();
            }
        });
        mDataBinding.tvAddLines.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAddLines://添加台词
                addDialog();
                myAddDialog.show();
                break;
            case R.id.tvDialogAddRight:
                saveLines();
                break;
            case R.id.tvDialogAddCancel:
                myAddDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        TaiciDbHelper.getTaiciBeans().observe(this, new Observer<List<TaiciBean>>() {
            @Override
            public void onChanged(List<TaiciBean> taiciBeans) {
                mTaiciBeanList = taiciBeans;
                if (mTaiciBeanList != null && mTaiciBeanList.size() > 0) {
                    linesAdapter.setmDatas(taiciBeans);
                    linesAdapter.notifyDataSetChanged();
                    mDataBinding.rvLinesList.setVisibility(View.VISIBLE);
                    mDataBinding.tvMyLinesNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvLinesList.setVisibility(View.GONE);
                    mDataBinding.tvMyLinesNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //保存台词
    private void saveLines() {
        String content = dialogText.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content);
        } else {
            TaiciBean saveBean = new TaiciBean();
            saveBean.setCreateTime(System.currentTimeMillis());
            saveBean.setTitle(content);
            saveBean.setContent(content);
            //新增
            TaiciDbHelper.insert(saveBean);
            ToastUtils.showShort(getString(R.string.save_success));
            myAddDialog.dismiss();
        }
    }

    private void addDialog() {
        myAddDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add, null);
        myAddDialog.setContentView(inflate);
        myAddDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myAddDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = (int) (dm.heightPixels - 50);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogAddCancel);
        TextView dialogRight = inflate.findViewById(R.id.tvDialogAddRight);
        dialogText = inflate.findViewById(R.id.etDialogAddContent);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
    }

}